package uk.co.richardwkirk.parkrun.geo;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;

import uk.co.richardwkirk.parkrun.data.events.Event;

public class LeafRegion extends Region {

	HashMap<String, Event> events = new HashMap<String, Event>();
	
	public Event getEvent(String name, boolean useFullName) {
		String key = name.toLowerCase();
		if (events.containsKey(key)) {
			return events.get(key);
		}
		else {
			// Look up by full name
			for (Event event : events.values()) {
				if (event.getFullName().equals(name)) {
					return event;
				}
			}
			
			// Event does not exist in this region
			return null;
		}
	}
	
	public int countEvents() {
		return events.size();
	}

	public void addRegion(Region region) throws GeoDataException {
		throw new GeoDataException("Cannot add sub-regions to a leaf region.");
	}
	
	public void addEvent(Event event) throws GeoDataException {
		events.put(event.getShortName().toLowerCase(), event);
		event.setRegion(this);
	}
	
	public Collection<Event> getAllEvents() {
		LinkedList<Event> allEvents = new LinkedList<Event>();
		allEvents.addAll(events.values());
		return allEvents;
	}
	
}
